//
//  DeliveryAddressViewController.m
//  cityStar
//
//  Created by joyban on 14-12-29.
//  Copyright (c) 2014年 tiny. All rights reserved.
//

#import "DeliveryAddressViewController.h"
#import "DeliveryAddressCell.h"

#import "UIColor+FlatUI.h"
#import "AddDeliveryViewController.h"

#import "DeliveryModel.h"
#import "ModelDelivery.h"

NSString *const deliveryAddressID = @"DeliveryAddressID";


@interface DeliveryAddressViewController ()<CSdidDelegate>
@property (weak, nonatomic) IBOutlet UITableView *DeliveryTable;
@property (strong, nonatomic) NSMutableArray *cellArray;
@property (strong, nonatomic) NSString *page;
@property (weak, nonatomic) IBOutlet UIButton *addbtn;

@property (nonatomic,strong) DeliveryModel *DeliveryModelMessage;
@property (nonatomic,strong) ModelDelivery *ModelDeliveryMessage;

@end

@implementation DeliveryAddressViewController

-(void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"收货地址";
    self.addbtn.layer.cornerRadius = 4;
    [self addBackButton];
    [self setUpTableViews];
    self.cellArray = [[NSMutableArray alloc]init];
    self.page = @"0";
}

-(void)viewWillAppear:(BOOL)animated
{
    [self webRequest_getDeliveryAddressWithDetailID:self.page];
}

#pragma mark - webRequest
-(void)webRequest_getDeliveryAddressWithDetailID:(NSString *)page
{
    NSString *postUrl = [NSString stringWithFormat:@"%@%@",KInterface_Address,page];
    [HttpRequestManager getMethod:postUrl parameters:nil completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
        if (success) {
            DLog(@"%@",response);
            self.DeliveryModelMessage = [DeliveryModel objectFromDictionary:response[@"value"]];
            [self.DeliveryTable reloadData];
        }else{
            [SVProgressHUD showErrorWithStatus:errMsg];
        }
    }];

}

-(void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

-(void)senderdidClick:(UIButton *)btn
{
    float index = btn.tag;
    self.ModelDeliveryMessage = [[ModelDelivery alloc]init];
    [self.DeliveryModelMessage.content enumerateObjectsUsingBlock:^(DeliverycontentModel *obj, NSUInteger idx, BOOL *stop) {
        NSLog(@"%lu",(unsigned long)idx);
        if(index == idx)
        {
            self.ModelDeliveryMessage.uuid = obj.uuid;
            self.ModelDeliveryMessage.city = obj.city;
            self.ModelDeliveryMessage.name = obj.name;
            self.ModelDeliveryMessage.address = obj.address;
            self.ModelDeliveryMessage.province = obj.province;
            self.ModelDeliveryMessage.district = obj.district;
            self.ModelDeliveryMessage.is_default = obj.is_default;
            self.ModelDeliveryMessage.zipcode = obj.zipcode;
            self.ModelDeliveryMessage.phone = obj.phone;
            self.ModelDeliveryMessage.mobile = obj.mobile;
            self.ModelDeliveryMessage.city_name = obj.city_name;
            self.ModelDeliveryMessage.district_name = obj.district_name;
            self.ModelDeliveryMessage.province_name = obj.province_name;
        }
    }];
    AddDeliveryViewController *addDeliveryVC = [[AddDeliveryViewController alloc]initWithName:self.ModelDeliveryMessage];
    [self.navigationController pushViewController:addDeliveryVC animated:YES];
}

-(void)setUpTableViews
{
    [self.DeliveryTable registerNib:[DeliveryAddressCell nib] forCellReuseIdentifier:deliveryAddressID];
}

#pragma mark - UITableViewDelegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [DeliveryAddressCell heightOfTableCell];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if(self.DeliveryModelMessage.total != nil)
    {
        return [self.DeliveryModelMessage.total integerValue];
    }else{
        return 0;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if(section ==0 )
    {
        return 35;
    }else{
        return 10;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView * sectionView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 220)] ;
    [sectionView setBackgroundColor:[UIColor colorFromHexCode:@"F6F5F1"]];
    if(section == 0)
    {
        UILabel * label = [[UILabel alloc] init] ;
        label.frame = CGRectMake(0, 0, tableView.bounds.size.width, 25);
        label.backgroundColor = [UIColor colorFromHexCode:@"F4EAD7"];
        label.textColor = [UIColor colorFromHexCode:@"333333"];
        label.font=[UIFont systemFontOfSize:13];;
        [sectionView addSubview:label];
        label.text = @"    最多可保存5个收货地址";
    }
    return sectionView;
}

- (IBAction)selectAddress:(id)sender {
    AddDeliveryViewController *addDeliveryVC = [[AddDeliveryViewController alloc]init];
    [self.navigationController pushViewController:addDeliveryVC animated:YES];
}

#pragma mark - UITableViewDataSource
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DeliveryAddressCell *deliveryAddressCell = [tableView dequeueReusableCellWithIdentifier:deliveryAddressID];
    deliveryAddressCell.selectionStyle = UITableViewCellSelectionStyleGray;
    deliveryAddressCell.selectImg.hidden = YES;
    if(indexPath.section == 1)
    {
        deliveryAddressCell.selectImg.hidden = NO;
    }
    deliveryAddressCell.delegate = self;
    [self.cellArray addObject:deliveryAddressCell];
    [deliveryAddressCell setWithCellData:self.DeliveryModelMessage setcellrow:indexPath.section];
    return deliveryAddressCell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    for(DeliveryAddressCell *deliveryCell in self.cellArray)
    {
        deliveryCell.selectImg.hidden = YES;
    }
    DeliveryAddressCell *deliveryAddressCell = (DeliveryAddressCell *)[tableView cellForRowAtIndexPath:indexPath];
    deliveryAddressCell.selectImg.hidden = NO;
    [self.DeliveryModelMessage.content enumerateObjectsUsingBlock:^(DeliverycontentModel *obj, NSUInteger idx, BOOL *stop) {
        if(indexPath.section == idx)
        {
            NSLog(@"%@,%@",obj.address,obj.uuid);
            [[NSNotificationCenter defaultCenter] postNotificationName:@"senderdid" object:obj.address];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"senderUUID" object:[NSString stringWithFormat:@"%@",obj.uuid]];
//            [self.delegate senderdidClick:obj.address];
//            [self.delegate senderUUIDlick:[NSString stringWithFormat:@"%@",obj.uuid]];
        }
    }];

     [self.navigationController popViewControllerAnimated:YES];
    [self.DeliveryModelMessage.content enumerateObjectsUsingBlock:^(DeliverycontentModel *obj, NSUInteger idx, BOOL *stop) {
        if(indexPath.section == idx)
        {
            NSString * uuid = [NSString stringWithFormat:@"%@",obj.uuid];
            NSDictionary *parameters = @{@"uuid":uuid};
            [HttpRequestManager putMethod:kInterface_defaul parameters:parameters completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
                if (success) {
                    [self.navigationController popViewControllerAnimated:YES];
                }else{
                    NSLog(@"%@",errMsg);
                    [SVProgressHUD showErrorWithStatus:errMsg];
                }
            }];
        }
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

